package com.jinlejia.erp.ui.house;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.Filter1Adapter;
import com.jinlejia.erp.adapter.Filter23Adapter;
import com.jinlejia.erp.adapter.FilterArea1Adapter;
import com.jinlejia.erp.adapter.FilterArea2Adapter;
import com.jinlejia.erp.adapter.FilterBussinesAdapter;
import com.jinlejia.erp.adapter.FilterSelectAdapter;
import com.jinlejia.erp.adapter.FilterTagMultiAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.bean.MoreSelectBean;
import com.jinlejia.erp.databinding.ActivityMenuMoreBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;

public class MenuMoreActivity extends BaseActivity implements Filter23Adapter.OnItemSelectedListener, Filter23Adapter.OnItemRemoveListener {

    //第一列
    Filter1Adapter filter1Adapter;
    ArrayList<String> f1Data = new ArrayList<>();
    //排序adapter
    Filter23Adapter sortAdapter;
    ArrayList<Map<String, Object>> sortData = new ArrayList<>();
    //片区
    FilterArea1Adapter area1Adapter;
    ArrayList<Map<String, Object>> areaData = new ArrayList<>();
    FilterArea2Adapter area2Adapter;
    ArrayList<String> area2Data = new ArrayList<>();
    //掉状态
    FilterArea1Adapter changeStateAdapter;
    ArrayList<Map<String, Object>> changeStateData = new ArrayList<>();
    FilterTagMultiAdapter chageStateDayAdapter;
    ArrayList<Map<String, Object>> chageStateDayData = new ArrayList<>();
    HashSet<String> chageStateSelectList = new HashSet<>();
    //商圈
    FilterBussinesAdapter bussinesAdapter;
    ArrayList<String> bussinesData = new ArrayList<>();
    //面积adapter
    Filter23Adapter squareAdapter;
    ArrayList<Map<String, Object>> squareData = new ArrayList<>();
    //楼层adapter
    Filter23Adapter floorAdapter;
    ArrayList<Map<String, Object>> floorData = new ArrayList<>();
    //朝向adapter
    Filter23Adapter orientationAdapter;
    ArrayList<Map<String, Object>> orientationData = new ArrayList<>();
    //家具Adapter
    FilterTagMultiAdapter houseFurnitureAdapter;
    ArrayList<Map<String, Object>> houseFurnitureData = new ArrayList<>();
    HashSet<String> houseFurnitureSelectList = new HashSet<>();
    //家电Adapter
    FilterTagMultiAdapter houseApplianceAdapter;
    ArrayList<Map<String, Object>> houseApplianceData = new ArrayList<>();
    HashSet<String> houseApplianceSelectList = new HashSet<>();
    //设施Adapter
    FilterTagMultiAdapter houseFacilitiesAdapter;
    ArrayList<Map<String, Object>> houseFacilitiesData = new ArrayList<>();
    HashSet<String> houseFacilitiesSelectList = new HashSet<>();
    //装修Adapter
    Filter23Adapter fitAdapter;
    ArrayList<Map<String, Object>> fitData = new ArrayList<>();
    //标签Adapter
    FilterTagMultiAdapter tagsAdapter;
    ArrayList<Map<String, Object>> tagsData = new ArrayList<>();
    HashSet<String> tagSelectList = new HashSet<>();
    //我的角色Adapter
    Filter23Adapter roleAdapter;
    ArrayList<Map<String, Object>> roleData = new ArrayList<>();
    //实勘Adapter
    Filter23Adapter investigationAdapter;
    ArrayList<Map<String, Object>> investigationData = new ArrayList<>();
    //交易属性Adapter
    Filter23Adapter propertyAdapter;
    ArrayList<Map<String, Object>> propertyData = new ArrayList<>();
    //房屋现状Adapter
    Filter23Adapter houseStatusAdapter;
    ArrayList<Map<String, Object>> houseStatusData = new ArrayList<>();
    //建筑类型Adapter
    Filter23Adapter buildingTypeAdapter;
    ArrayList<Map<String, Object>> buildingTypeData = new ArrayList<>();

    ArrayList<MoreSelectBean> filterSelected = new ArrayList<>();
    FilterSelectAdapter selectAdapter;

    JSONObject jsonObject = new JSONObject();
    String area = "", countyName = "", changeStateName = "", chageStateDay;
    private String customFloor = "";

    private ActivityMenuMoreBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_menu_more);

        binding.btBack.setOnClickListener(this::onClick);
        binding.btSave.setOnClickListener(this::onClick);
        binding.btClear.setOnClickListener(this::onClick);

        if (getIntent().hasExtra("chageStateSelectList")) {
            ArrayList<String> chageStateSelectArraylist = getIntent().getStringArrayListExtra("chageStateSelectList");

            for (String s : chageStateSelectArraylist) {
                chageStateSelectList.add(s);
            }
        }
        if (getIntent().hasExtra("selectList"))
            filterSelected = (ArrayList<MoreSelectBean>) getIntent().getSerializableExtra("selectList");
        if (getIntent().hasExtra("jsonString") && !TextUtils.isEmpty(getIntent().getStringExtra("jsonString"))) {
            jsonObject = JSONObject.parseObject(getIntent().getStringExtra("jsonString"));
            String tags = jsonObject.getString("tagList");
            if (!TextUtils.isEmpty(tags))
                tagSelectList.addAll(Arrays.asList(tags.split(";")));
            if (jsonObject.containsKey("county"))
                countyName = jsonObject.get("county").toString();
            if (jsonObject.containsKey("area"))
                area = jsonObject.get("area").toString();
            if (jsonObject.containsKey("changeStateType")) {
                if (getIntent().getStringExtra("type").equals("shs") && jsonObject.get("changeStateType").toString().equals("0")) {
                    changeStateName = "离掉未知天数";
                } else if (!getIntent().getStringExtra("type").equals("shs") && jsonObject.get("changeStateType").toString().equals("0")) {
                    changeStateName = "离掉维护天数";
                } else {
                    changeStateName = "离掉暂缓天数";
                }
            }
        }
        initView();
        getDictionary();
    }

    private void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        binding.recyclerSelected.setLayoutManager(gridLayoutManager);
        selectAdapter = new FilterSelectAdapter(filterSelected);
        binding.recyclerSelected.setAdapter(selectAdapter);
        selectAdapter.setOnDataSizeChangeListener(size -> {
            if (size >= 1) {
                binding.selectedLayout.setVisibility(View.VISIBLE);
                binding.btClear.setVisibility(View.VISIBLE);
            } else {
                binding.btClear.setVisibility(View.GONE);
            }
        });
        selectAdapter.setOnItemClickListener((view, o) -> {
            MoreSelectBean msb = (MoreSelectBean) o;
            filterSelected.remove(msb);
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
            jsonObject.remove(msb.getType());
            switch (msb.getType()) {
                case "sortBy":
                    jsonObject.remove("sortBy");
                    jsonObject.remove("sortKey");
                    sortAdapter.cur_position = -1;
                    sortAdapter.notifyDataSetChanged();
                    break;

                case "changeState":
                    jsonObject.remove("changeStateType");
                    jsonObject.remove("changeStateDays");
                    chageStateDayAdapter.cur_position = -1;
                    String name = msb.getName();
                    String s = truncateHeadString(name, 6);
                    chageStateSelectList.remove(s);
                    if (chageStateSelectList.isEmpty()) {
                        changeStateAdapter.cur_position = -1;
                        changeStateAdapter.notifyDataSetChanged();
                    }
                    chageStateDayAdapter.notifyDataSetChanged();
                    break;
                case "county":
                    jsonObject.remove("county");
                    jsonObject.remove("area");
                    area1Adapter.cur_position = -1;
                    area2Adapter.cur_position = -1;
                    area1Adapter.notifyDataSetChanged();
                    area2Adapter.notifyDataSetChanged();
                    break;
                case "area":
                    jsonObject.remove("county");
                    jsonObject.remove("area");
                    area1Adapter.cur_position = -1;
                    area2Adapter.cur_position = -1;
                    area1Adapter.notifyDataSetChanged();
                    area2Adapter.notifyDataSetChanged();
                    break;
                case "bussiness":
                    bussinesAdapter.cur_position = -1;
                    bussinesAdapter.notifyDataSetChanged();
                    break;
                case "square":
                    jsonObject.remove("square_start");
                    jsonObject.remove("square_end");
                    squareAdapter.cur_position = -1;
                    squareAdapter.notifyDataSetChanged();
                    break;
                case "floorState":
                    jsonObject.remove("floor_start");
                    jsonObject.remove("floor_end");
                    floorAdapter.cur_position = -1;
                    floorAdapter.notifyDataSetChanged();
                    break;
                case "face":
                    orientationAdapter.cur_position = -1;
                    orientationAdapter.notifyDataSetChanged();
                    break;
                case "houseFurniture":
                    //家具
                    houseFurnitureSelectList.remove(msb.getName());
                    houseFurnitureAdapter.setSelectList(houseFurnitureSelectList);
                    houseFurnitureAdapter.notifyDataSetChanged();
                    break;
                case "houseAppliance":
                    //家电
                    houseApplianceSelectList.remove(msb.getName());
                    houseApplianceAdapter.setSelectList(houseApplianceSelectList);
                    houseApplianceAdapter.notifyDataSetChanged();
                    break;
                case "houseFacilities":
                    //设施
                    houseFacilitiesSelectList.remove(msb.getName());
                    houseFacilitiesAdapter.setSelectList(houseFacilitiesSelectList);
                    houseFacilitiesAdapter.notifyDataSetChanged();
                    break;
                case "fit":
                    //装修
                    fitAdapter.cur_position = -1;
                    fitAdapter.notifyDataSetChanged();
                    break;
                case "tag":
                    //去除重复
                    tagSelectList.remove(msb.getName());
                    tagsAdapter.setSelectList(tagSelectList);
                    tagsAdapter.notifyDataSetChanged();
                    break;
                case "role":
                    roleAdapter.cur_position = -1;
                    roleAdapter.notifyDataSetChanged();
                    break;
                case "isSurvey":
                    investigationAdapter.cur_position = -1;
                    investigationAdapter.notifyDataSetChanged();
                    break;
                case "title":
                    propertyAdapter.cur_position = -1;
                    propertyAdapter.notifyDataSetChanged();
                    break;
                case "live":
                    houseStatusAdapter.cur_position = -1;
                    houseStatusAdapter.notifyDataSetChanged();
                    break;
                case "buildingType":
                    buildingTypeAdapter.cur_position = -1;
                    buildingTypeAdapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
        });


        binding.recycler1.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler2.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler3.setLayoutManager(new LinearLayoutManager(this));

        //第一列RecyclerView
        if (f1Data.size() == 0)
            f1Data.addAll(Arrays.asList(getResources().getStringArray(R.array.filter)));
        filter1Adapter = new Filter1Adapter(f1Data);
        //首次进入显示
        changeStateAdapter = new FilterArea1Adapter(changeStateData);
        chageStateDayAdapter = new FilterTagMultiAdapter(chageStateDayData, chageStateSelectList);
        binding.recycler3.setVisibility(View.VISIBLE);
        binding.recycler3.setAdapter(chageStateDayAdapter);
        binding.recycler2.setAdapter(changeStateAdapter);

        //排序
        sortAdapter = new Filter23Adapter(sortData, "sortBy", filterSelected);
        sortAdapter.setOnItemSelectedListener(this);
        sortAdapter.setOnItemRemoveListener(this);
        //掉状态
        changeStateAdapter.setCounty(changeStateName);
        changeStateAdapter.setOnItemSelectedListener(map -> {
            chageStateDayData.clear();
            ArrayList<Integer> changeStateList = ((ArrayList<Integer>) JSON.parseArray(changeStateData.get(changeStateAdapter.cur_position).get("dayList").toString(), Integer.class));
            for (Integer change : changeStateList) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("valueName", change.toString());
                chageStateDayData.add(map1);
            }
            chageStateDayAdapter.setData(chageStateDayData);
            binding.recycler3.setAdapter(chageStateDayAdapter);
            chageStateDayAdapter.notifyDataSetChanged();

            changeStateName = map.get("changeStateName").toString();
            chageStateDay = "";

            if (changeStateName.equals("离掉未知天数") || changeStateName.equals("离掉维护天数")) {
                jsonObject.put("changeStateType", 0);
            } else if (changeStateName.equals("离掉暂缓天数")) {
                jsonObject.put("changeStateType", 1);
            }

            jsonObject.remove("changeStateDay");
        });

        changeStateAdapter.setOnItemRemoveListener(map -> {
            Iterator<MoreSelectBean> iterator = filterSelected.iterator();
            while (iterator.hasNext()) {
                MoreSelectBean bean = iterator.next();
                if (bean.getType().equals("changeState")) {
                    iterator.remove();
                }
            }

            changeStateName = "";
            chageStateDay = "";
            jsonObject.remove("changeStateType");
            jsonObject.remove("changeStateDays");
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();

            chageStateSelectList.clear();
            chageStateDayAdapter.notifyDataSetChanged();
        });

        chageStateDayAdapter.setOnItemSelectedListener(new FilterTagMultiAdapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String item) {
                chageStateDay = changeStateName + item;
                chageStateSelectList.add(item);
                MoreSelectBean changeStateDayslianceMSB = new MoreSelectBean("changeState", chageStateDay);
                filterSelected.add(changeStateDayslianceMSB);
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemRemove(String item) {
                chageStateSelectList.remove(item);
                Iterator<MoreSelectBean> iterator = filterSelected.iterator();
                while (iterator.hasNext()) {
                    MoreSelectBean bean = iterator.next();
                    if (bean.getName().equals(changeStateName + item)) {
                        iterator.remove();
                    }
                }


                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
                if (jsonObject.size() > 0 || filterSelected.size() > 0) {
                    binding.selectedLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.selectedLayout.setVisibility(View.GONE);
                }
            }
        });

        //片区
        area1Adapter = new FilterArea1Adapter(areaData);
        area1Adapter.setCounty(countyName);
        area2Adapter = new FilterArea2Adapter(area2Data);
        area2Adapter.setArea(area);
        binding.recycler3.setVisibility(View.VISIBLE);
        binding.recycler3.setAdapter(area2Adapter);
        area1Adapter.setOnItemSelectedListener(map -> {
            area2Data.clear();
            area2Data.addAll(JSON.parseArray(map.get("areaList").toString(), String.class));
            area2Adapter.setData(area2Data);
            if (!area2Data.contains(area2Adapter.area)) {
                area2Adapter.cur_position = -1;
            }
            for (int i = 0; i < filterSelected.size(); i++) {
                if (filterSelected.get(i).getType().equals("county")) {
                    filterSelected.remove(filterSelected.get(i));
                    break;
                }
                if (filterSelected.get(i).getType().equals("area"))
                    filterSelected.remove(filterSelected.get(i));
            }
            countyName = map.get("countyName").toString();
            area = "";
            MoreSelectBean msb = new MoreSelectBean("county", countyName);
            filterSelected.add(msb);
            jsonObject.put("county", countyName);
            jsonObject.remove("area");
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });
        area1Adapter.setOnItemRemoveListener(map -> {
            for (int i = 0; i < filterSelected.size(); i++) {
                if (filterSelected.get(i).getType().equals("county")) {
                    filterSelected.remove(filterSelected.get(i));
                    break;
                }
                if (filterSelected.get(i).getType().equals("area"))
                    filterSelected.remove(filterSelected.get(i));
            }
            countyName = "";
            area = "";
            jsonObject.remove("county");
            jsonObject.remove("area");
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
            area2Adapter.cur_position = -1;
            area2Adapter.notifyDataSetChanged();
        });
        area2Adapter.setOnItemSelectedListener(s -> {
            jsonObject.put("area", s);
            area = s;
            //选择片区后，删除区县
            for (int i = 0; i < filterSelected.size(); i++) {
                if (filterSelected.get(i).getType().equals("county")) {
                    filterSelected.remove(filterSelected.get(i));
                    break;
                }
                if (filterSelected.get(i).getType().equals("area"))
                    filterSelected.remove(filterSelected.get(i));
            }
            MoreSelectBean areaMSB = new MoreSelectBean("area", s);
            filterSelected.add(areaMSB);
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });
        area2Adapter.setOnItemRemoveListener(s -> {
            jsonObject.remove("area");

            for (int i = 0; i < filterSelected.size(); i++) {
                if (filterSelected.get(i).getType().equals("county")) {
                    filterSelected.remove(filterSelected.get(i));
                    break;
                }
                if (filterSelected.get(i).getType().equals("area"))
                    filterSelected.remove(filterSelected.get(i));
            }
            MoreSelectBean msb = new MoreSelectBean("county", countyName);
            filterSelected.add(msb);
            jsonObject.put("county", countyName);
            area = "";
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });
        //商圈
        bussinesAdapter = new FilterBussinesAdapter(bussinesData, filterSelected);
        bussinesAdapter.setOnItemSelectedListener(str -> {
            jsonObject.put("bussiness", str);
            for (MoreSelectBean msb : filterSelected) {
                if (msb.getType().equals("bussiness")) {
                    filterSelected.remove(msb);
                    break;
                }
            }
            MoreSelectBean bMSB = new MoreSelectBean("bussiness", str);
            filterSelected.add(bMSB);
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });
        bussinesAdapter.setOnItemRemoveListener(str -> {
            jsonObject.put("bussiness", "");
            for (MoreSelectBean msb : filterSelected) {
                if (msb.getType().equals("bussiness")) {
                    filterSelected.remove(msb);
                    break;
                }
            }
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });
        //面积
        squareAdapter = new Filter23Adapter(squareData, "square", filterSelected);
        squareAdapter.setOnItemSelectedListener(this);
        squareAdapter.setOnItemRemoveListener(this);
        squareAdapter.setOnConfirmListener((min, max) -> {
            if (jsonObject.containsKey("square_start") && !TextUtils.isEmpty(jsonObject.get("square_start").toString())) {
                for (MoreSelectBean msb : filterSelected) {
                    if (msb.getType().equals("square")) {
                        filterSelected.remove(msb);
                        break;
                    }
                }
            }
            squareAdapter.cur_position = -1;
            squareAdapter.notifyDataSetChanged();
            jsonObject.put("square_start", min);
            jsonObject.put("square_end", max);
            String str = min + "-" + max + "平米";
            MoreSelectBean squareMSB = new MoreSelectBean("square", str);
            filterSelected.add(squareMSB);

            if (jsonObject.size() > 0) {
                binding.selectedLayout.setVisibility(View.VISIBLE);
            }
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });

        //楼层
        floorAdapter = new Filter23Adapter(floorData, "floorState", filterSelected);
        floorAdapter.setOnItemSelectedListener(this);
        floorAdapter.setOnItemRemoveListener(this);
        floorAdapter.setOnConfirmListener((min, max) -> {
            if (jsonObject.containsKey("floorState") || jsonObject.containsKey("floor_start")) {
                for (MoreSelectBean msb : filterSelected) {
                    if (msb.getType().equals("floorState")) {
                        filterSelected.remove(msb);
                        break;
                    }
                }
            }
            floorAdapter.cur_position = -1;
            floorAdapter.notifyDataSetChanged();
            jsonObject.remove("floorState");
            jsonObject.put("floor_start", min);
            jsonObject.put("floor_end", max);
            customFloor = min + "-" + max + "层";
            MoreSelectBean floorMSB = new MoreSelectBean("floorState", customFloor);
            filterSelected.add(floorMSB);

            if (jsonObject.size() > 0) {
                binding.selectedLayout.setVisibility(View.VISIBLE);
            }
            selectAdapter.setData(filterSelected);
            selectAdapter.notifyDataSetChanged();
        });

        //朝向
        orientationAdapter = new Filter23Adapter(orientationData, "face", filterSelected);
        orientationAdapter.setOnItemSelectedListener(this);
        orientationAdapter.setOnItemRemoveListener(this);

        //家具
        houseFurnitureAdapter = new FilterTagMultiAdapter(houseFurnitureData, houseFurnitureSelectList);
        houseFurnitureAdapter.setOnItemSelectedListener(new FilterTagMultiAdapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String item) {
//                jsonObject.put(item,item);
                houseFurnitureSelectList.add(item);
                MoreSelectBean houseFurnitureMSB = new MoreSelectBean("houseFurniture", item);
                filterSelected.add(houseFurnitureMSB);
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemRemove(String item) {
                //
//                tagSelectList = new ArrayList<String>(new HashSet<String>(tagSelectList));
//                jsonObject.remove(item);
                houseFurnitureSelectList.remove(item);
                for (int i = 0; i < filterSelected.size(); i++) {
                    if (filterSelected.get(i).getType().equals("houseFurniture") && filterSelected.get(i).getName().equals(item)) {
                        filterSelected.remove(i);
                    }
                }
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
                if (jsonObject.size() > 0 || filterSelected.size() > 0) {
                    binding.selectedLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.selectedLayout.setVisibility(View.GONE);
                }
            }
        });

        //家电
        houseApplianceAdapter = new FilterTagMultiAdapter(houseApplianceData, houseApplianceSelectList);
        houseApplianceAdapter.setOnItemSelectedListener(new FilterTagMultiAdapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String item) {
//                jsonObject.put(item,item);
                houseApplianceSelectList.add(item);
                MoreSelectBean houseApplianceMSB = new MoreSelectBean("houseAppliance", item);
                filterSelected.add(houseApplianceMSB);
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemRemove(String item) {
                //
//                tagSelectList = new ArrayList<String>(new HashSet<String>(tagSelectList));
//                jsonObject.remove(item);
                houseApplianceSelectList.remove(item);
                for (int i = 0; i < filterSelected.size(); i++) {
                    if (filterSelected.get(i).getType().equals("houseAppliance") && filterSelected.get(i).getName().equals(item)) {
                        filterSelected.remove(i);
                    }
                }
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
                if (jsonObject.size() > 0 || filterSelected.size() > 0) {
                    binding.selectedLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.selectedLayout.setVisibility(View.GONE);
                }
            }
        });

        //设施
        houseFacilitiesAdapter = new FilterTagMultiAdapter(houseFacilitiesData, houseFacilitiesSelectList);
        houseFacilitiesAdapter.setOnItemSelectedListener(new FilterTagMultiAdapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String item) {
//                jsonObject.put(item,item);
                houseFacilitiesSelectList.add(item);
                MoreSelectBean houseFacilitiesMSB = new MoreSelectBean("houseFacilities", item);
                filterSelected.add(houseFacilitiesMSB);
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemRemove(String item) {
                //
//                tagSelectList = new ArrayList<String>(new HashSet<String>(tagSelectList));
//                jsonObject.remove(item);
                houseFacilitiesSelectList.remove(item);
                for (int i = 0; i < filterSelected.size(); i++) {
                    if (filterSelected.get(i).getType().equals("houseFacilities") && filterSelected.get(i).getName().equals(item)) {
                        filterSelected.remove(i);
                    }
                }
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
                if (jsonObject.size() > 0 || filterSelected.size() > 0) {
                    binding.selectedLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.selectedLayout.setVisibility(View.GONE);
                }
            }
        });

        //装修
        fitAdapter = new Filter23Adapter(fitData, "fit", filterSelected);
        fitAdapter.setOnItemSelectedListener(this);
        fitAdapter.setOnItemRemoveListener(this);

        //标签
        tagsAdapter = new FilterTagMultiAdapter(tagsData, tagSelectList);
        tagsAdapter.setOnItemSelectedListener(new FilterTagMultiAdapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String item) {
//                jsonObject.put(item,item);
                tagSelectList.add(item);
                MoreSelectBean tagMSB = new MoreSelectBean("tag", item);
                filterSelected.add(tagMSB);
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemRemove(String item) {
                //
//                tagSelectList = new ArrayList<String>(new HashSet<String>(tagSelectList));
//                jsonObject.remove(item);
                tagSelectList.remove(item);
                for (int i = 0; i < filterSelected.size(); i++) {
                    if (filterSelected.get(i).getType().equals("tag") && filterSelected.get(i).getName().equals(item)) {
                        filterSelected.remove(i);
                    }
                }
                selectAdapter.setData(filterSelected);
                selectAdapter.notifyDataSetChanged();
                if (jsonObject.size() > 0 || filterSelected.size() > 0) {
                    binding.selectedLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.selectedLayout.setVisibility(View.GONE);
                }
            }
        });

        //角色
        roleAdapter = new Filter23Adapter(roleData, "role", filterSelected);
        roleAdapter.setOnItemSelectedListener(this);
        roleAdapter.setOnItemRemoveListener(this);

        //实勘
        investigationAdapter = new Filter23Adapter(investigationData, "isSurvey", filterSelected);
        investigationAdapter.setOnItemSelectedListener(this);
        investigationAdapter.setOnItemRemoveListener(this);

        //交易属性
        propertyAdapter = new Filter23Adapter(propertyData, "title", filterSelected);
        propertyAdapter.setOnItemSelectedListener(this);
        propertyAdapter.setOnItemRemoveListener(this);

        //房屋现状
        houseStatusAdapter = new Filter23Adapter(houseStatusData, "live", filterSelected);
        houseStatusAdapter.setOnItemSelectedListener(this);
        houseStatusAdapter.setOnItemRemoveListener(this);

        //建筑类型
        buildingTypeAdapter = new Filter23Adapter(buildingTypeData, "buildingType", filterSelected);
        buildingTypeAdapter.setOnItemSelectedListener(this);
        buildingTypeAdapter.setOnItemRemoveListener(this);


        binding.recycler1.setAdapter(filter1Adapter);
        filter1Adapter.setOnItemSelectedListener(str -> {
            if (str.equals("掉状态")) {
                binding.recycler3.setVisibility(View.VISIBLE);
                binding.recycler2.setAdapter(changeStateAdapter);
                binding.recycler3.setAdapter(chageStateDayAdapter);
            } else if (str.equals("排序")) {
                binding.recycler2.setAdapter(sortAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("片区")) {
                binding.recycler3.setVisibility(View.VISIBLE);
                binding.recycler2.setAdapter(area1Adapter);
                binding.recycler3.setAdapter(area2Adapter);
            } else if (str.equals("商圈")) {
                binding.recycler2.setAdapter(bussinesAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("面积")) {
                squareAdapter.addFooter("面积");
                binding.recycler2.setAdapter(squareAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("楼层")) {
                floorAdapter.addFooter("楼层");
                binding.recycler2.setAdapter(floorAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("朝向")) {
                binding.recycler2.setAdapter(orientationAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("家具")) {
                binding.recycler2.setAdapter(houseFurnitureAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("家电")) {
                binding.recycler2.setAdapter(houseApplianceAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("设施")) {
                binding.recycler2.setAdapter(houseFacilitiesAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("装修")) {
                binding.recycler2.setAdapter(fitAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("标签")) {
                binding.recycler2.setAdapter(tagsAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("我的角色")) {
                binding.recycler2.setAdapter(roleAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("实勘")) {
                binding.recycler2.setAdapter(investigationAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("交易权属")) {
                binding.recycler2.setAdapter(propertyAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("房屋现状")) {
                binding.recycler2.setAdapter(houseStatusAdapter);
                binding.recycler3.setVisibility(View.GONE);
            } else if (str.equals("建筑类型")) {
                binding.recycler2.setAdapter(buildingTypeAdapter);
                binding.recycler3.setVisibility(View.GONE);
            }
        });
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_save:
                if (tagSelectList.size() > 0) {
                    jsonObject.put("tagList", getTagStringList(tagSelectList));
                } else {
                    jsonObject.remove("tagList");
                }
                if (houseFurnitureSelectList.size() > 0) {
                    jsonObject.put("houseFurniture", getTagStringList(houseFurnitureSelectList));
                } else {
                    jsonObject.remove("houseFurniture");
                }
                if (houseApplianceSelectList.size() > 0) {
                    jsonObject.put("houseAppliance", getTagStringList(houseApplianceSelectList));
                } else {
                    jsonObject.remove("houseAppliance");
                }
                if (chageStateSelectList.size() > 0) {
                    jsonObject.put("changeStateDays", getTagStringList(chageStateSelectList));
                    if (changeStateName.equals("离掉未知天数") || changeStateName.equals("离掉维护天数")) {
                        jsonObject.put("changeStateType", 0);
                    } else if (changeStateName.equals("离掉暂缓天数")) {
                        jsonObject.put("changeStateType", 1);
                    }
                } else {
                    jsonObject.remove("changeStateDays");
                }
                if (houseFacilitiesSelectList.size() > 0) {
                    jsonObject.put("houseFacilities", getTagStringList(houseFacilitiesSelectList));
                } else {
                    jsonObject.remove("houseFacilities");
                }


                Intent data = new Intent();
                if (chageStateSelectList.size() > 0) {
                    ArrayList<String> changeStateSelectArraylist = new ArrayList();
                    for (String s : chageStateSelectList) {
                        changeStateSelectArraylist.add(s);
                    }
                    data.putExtra("chageStateSelectList", changeStateSelectArraylist);
                }
                data.putExtra("selectList", filterSelected);
                data.putExtra("jsonString", jsonObject.toJSONString());
                data.putExtra("type", getIntent().getStringExtra("type"));
                setResult(RESULT_OK, data);
                finish();
                break;
            case R.id.bt_clear:
                filterSelected.clear();
                jsonObject.clear();
                tagSelectList.clear();
                chageStateSelectList.clear();
                binding.selectedLayout.setVisibility(View.GONE);
                selectAdapter.notifyDataSetChanged();
                binding.btClear.setVisibility(View.GONE);
                initView();
                break;
            default:
                break;
        }
    }

    private String getTagStringList(HashSet<String> list) {
        StringBuffer sb = new StringBuffer();
        for (String str : list) {
            sb = sb.append(";").append(str);
        }
        return sb.toString().replaceFirst(";", "");
    }

    @Override
    public void onItemRemove(Map<String, Object> map, String type) {
        for (MoreSelectBean msb : filterSelected) {
            if (msb.getType().equals(type)) {
                filterSelected.remove(msb);
                break;
            }
        }
        selectAdapter.setData(filterSelected);
        selectAdapter.notifyDataSetChanged();
        if (type.equals("square")) {
            jsonObject.remove("square_start");
            jsonObject.remove("square_end");
        } else if (type.equals("sortBy")) {
            //排序
            jsonObject.remove("sortBy");
            jsonObject.remove("sortKey");
        } else if (type.equals("floorState")) {
            jsonObject.remove("floor_start");
            jsonObject.remove("floor_end");
            jsonObject.remove("floorState");
        } else {
            jsonObject.remove(type);
            if (jsonObject.isEmpty()) {
                binding.selectedLayout.setVisibility(View.GONE);
                selectAdapter.notifyDataSetChanged();
                binding.btClear.setVisibility(View.GONE);
            }
        }
        if (jsonObject.size() > 0) {
            binding.selectedLayout.setVisibility(View.VISIBLE);
        } else {
            binding.selectedLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemSelected(Map<String, Object> map, String type) {
        if (type.equals("square")) {
            if (jsonObject.containsKey("square_start") && !TextUtils.isEmpty(jsonObject.get("square_start").toString())) {
                for (MoreSelectBean msb : filterSelected) {
                    if (msb.getType().equals("square")) {
                        filterSelected.remove(msb);
                        break;
                    }
                }
            }
            jsonObject.put("square_start", map.get("minValue").toString());
            jsonObject.put("square_end", map.get("maxValue").toString());
        } else if (type.equals("sortBy")) {
            //排序
            String[] str = map.get("valuel").toString().split("-");
            jsonObject.put("sortBy", str[1]);
            jsonObject.put("sortKey", str[0]);
        } else if (type.equals("floorState")) {
            //删除已选
            if (jsonObject.containsKey("floorState") || jsonObject.containsKey("floor_start")) {
                for (MoreSelectBean msb : filterSelected) {
                    if (msb.getType().equals("floorState")) {
                        filterSelected.remove(msb);
                        break;
                    }
                }
            }
            jsonObject.remove("floor_start");
            jsonObject.remove("floor_end");
            jsonObject.put("floorState", map.get("valuel").toString());

            customFloor = map.get("valueName").toString();
        } else {
            jsonObject.put(type, map.get("valuel").toString());
        }

        MoreSelectBean msb = new MoreSelectBean(type, map.get("valueName").toString());
        filterSelected.add(msb);
        selectAdapter.setData(filterSelected);
        selectAdapter.notifyDataSetChanged();

        if (jsonObject.size() > 0) {
            binding.selectedLayout.setVisibility(View.VISIBLE);
        } else {
            binding.selectedLayout.setVisibility(View.GONE);
        }

    }

    private void getDictionary() {

        OkHttpUtils.get()
                .url(ApiConfig.HOUSE_DICTIONARY.concat(getIntent().getStringExtra("type")))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(MenuMoreActivity.this, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            Log.e("wwe", "onResponse: " + data.getJSONArray("changeStateList").toString());
                            //排序
                            sortData.clear();
                            sortData = JSON.parseObject(data.getJSONArray("sortList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            sortAdapter.setData(sortData);
                            //片区
                            areaData.clear();
                            areaData = JSON.parseObject(data.getJSONArray("countyList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            area1Adapter.setData(areaData);
                            if (!TextUtils.isEmpty(countyName)) {
                                area2Data.addAll(JSON.parseArray(areaData.get(area1Adapter.cur_position).get("areaList").toString(), String.class));
                                area2Adapter.setData(area2Data);
                            }
                            //掉状态
                            changeStateData.clear();
                            changeStateData = JSON.parseObject(data.getJSONArray("changeStateList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            changeStateAdapter.setData(changeStateData);
                            if (!TextUtils.isEmpty(changeStateName)) {
                                ArrayList<Integer> changeStateList = ((ArrayList<Integer>) JSON.parseArray(changeStateData.get(changeStateAdapter.cur_position).get("dayList").toString(), Integer.class));
                                for (Integer change : changeStateList) {
                                    Map<String, Object> map = new HashMap<>();
                                    map.put("valueName", change.toString());
                                    chageStateDayData.add(map);
                                }
                                chageStateDayAdapter.setData(chageStateDayData);
                            }
                            //商圈
                            JSONArray bussines = JSON.parseArray(data.getJSONArray("bussinessList").toString());
                            bussinesData.clear();
                            bussinesData.addAll(JSON.parseArray(bussines.toJSONString(), String.class));
                            bussinesAdapter.setData(bussinesData);
                            //面积
                            squareData.clear();
                            squareData = JSON.parseObject(data.getJSONArray("squareList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            squareAdapter.setData(squareData);

                            //楼层
                            floorData.clear();
                            floorData = JSON.parseObject(data.getJSONArray("floorList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            floorAdapter.setData(floorData);
                            //朝向
                            orientationData.clear();
                            orientationData = JSON.parseObject(data.getJSONArray("faceList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            orientationAdapter.setData(orientationData);
                            //家具
                            houseFurnitureData.clear();
                            houseFurnitureData = JSON.parseObject(data.getJSONArray("houseFurniture").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            houseFurnitureAdapter.setData(houseFurnitureData);
                            //家电
                            houseApplianceData.clear();
                            houseApplianceData = JSON.parseObject(data.getJSONArray("houseAppliance").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            houseApplianceAdapter.setData(houseApplianceData);
                            //设施
                            houseFacilitiesData.clear();
                            houseFacilitiesData = JSON.parseObject(data.getJSONArray("houseFacilities").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            houseFacilitiesAdapter.setData(houseFacilitiesData);
                            //标签
                            tagsData.clear();
                            tagsData = JSON.parseObject(data.getJSONArray("tagList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            tagsAdapter.setData(tagsData);
                            //装修
                            fitData.clear();
                            fitData = JSON.parseObject(data.getJSONArray("fit").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            fitAdapter.setData(fitData);
                            //角色
                            roleData.clear();
                            roleData = JSON.parseObject(data.getJSONArray("roleList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            roleAdapter.setData(roleData);
                            //实勘
                            investigationData.clear();
                            Map<String, Object> map = new HashMap<>();
                            map.put("valueName", "无实勘");
                            map.put("valuel", 0);
                            Map<String, Object> map1 = new HashMap<>();
                            map1.put("valueName", "有实勘");
                            map1.put("valuel", 1);
                            investigationData.add(map);
                            investigationData.add(map1);
                            investigationAdapter.setData(investigationData);
                            //属性
                            propertyData.clear();
                            propertyData = JSON.parseObject(data.getJSONArray("titleList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            propertyAdapter.setData(propertyData);
                            //房屋现状
                            houseStatusData.clear();
                            houseStatusData = JSON.parseObject(data.getJSONArray("houseStatusList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            houseStatusAdapter.setData(houseStatusData);
                            //建筑类型
                            buildingTypeData.clear();
                            buildingTypeData = JSON.parseObject(data.getJSONArray("buildingType").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            buildingTypeAdapter.setData(buildingTypeData);
                        }
                    }
                });
    }


    public static String truncateHeadString(String origin, int count) {
        if (origin == null || origin.length() < count) {
            return null;
        }
        char[] arr = origin.toCharArray();
        char[] ret = new char[arr.length - count];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = arr[i + count];
        }
        return String.copyValueOf(ret);
    }
}
